<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;
if (isset($_POST['action']) && $_POST['action'] == "add")
{
	$address = trim(filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES));
	$place = trim(filter_var($_POST['place'],FILTER_SANITIZE_MAGIC_QUOTES));
	$currency = trim(filter_var($_POST['currency'],FILTER_SANITIZE_MAGIC_QUOTES));
	$work_time = trim(filter_var($_POST['work_time'],FILTER_SANITIZE_MAGIC_QUOTES));
	$add_query = "insert into `atm` (`bank_id`,`place`,`address`,`currency`,`work_time`) values ('$_POST[bank]','$place','$address','$currency','$work_time')";
	if (mysql_query($add_query)) {
		$atm = mysql_fetch_array(mysql_query("select * from `atm` order by `atm_id` desc limit 1"));
		to("/admin/atms/edit/$atm[atm_id]");
	} else {
		$add_error = true;
	}
}

require_once ADMIN.HEADFILE;
?>
<div align="left">
<h1>Добавить банкомат</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if (isset($_POST['bank']) && $bank_row['bank_id'] == $_POST['bank'])
					echo " selected='selected'";
				elseif (isset($_GET['bank_id']) && $bank_row['bank_id'] == $_GET['bank_id'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td width="150px">Адрес</td>
		<td width="500px">
			<input style='width:100%' type='text' name='address' value='<?php if (isset($_POST['address'])) echo $_POST['address'];?>' />
		</td>
	</tr>
	<tr>
		<td width="150px">Объект</td>
		<td width="500px">
			<input style='width:100%' type='text' name='place' value='<?php if (isset($_POST['place'])) echo $_POST['place'];?>' />
		</td>
	</tr>
	<tr>
		<td>Валюта</td>
		<td><input style="width:100%" type='text' name='currency' value='<?php
			if (isset($_POST['currency'])) {
				echo $_POST['currency'];
			} else {
				echo "RUR, USD, EUR";
			}
			?>' /></td>
	</tr>
	<tr>
		<td>Время работы</td>
		<td><input style='width:100%' type='text' name='work_time' value='<?php
			if (isset($_POST['work_time'])) {
				echo $_POST['work_time'];
			} else {
				echo "ежедневно: круглосуточно";
			}
			?>' /></td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>